#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@File    ：pyecharts_page.py
@Author  ：Juff
@Date    ：2023/12/29 16:02
'''
import clickhouse_driver
from pyecharts import options as opts
from pyecharts.charts import Bar, Grid, Line, Liquid, Page, Pie
from pyecharts.commons.utils import JsCode
from pyecharts.components import Table
from pyecharts.faker import Faker


def pie_qujian(qujian_list,count_list):
    c = (
        Pie()
        .add("", [list(z) for z in zip(qujian_list, count_list)])
        .set_global_opts(title_opts=opts.TitleOpts(title="区间饼状图"))
        .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
    )
    return c

def bar_xianlu(xianlu_list,avg_time_list):
    c = (
        Bar()
        .add_xaxis(xianlu_list)
        .add_yaxis("线路平均耗时", avg_time_list, category_gap="80%")
        .set_global_opts(title_opts=opts.TitleOpts(title="智慧地铁指标"))
    )
    return c

def line_szb(short_stations_list,count_list):
    c = (
        Line()
        .add_xaxis(short_stations_list)
        .add_yaxis("区间", count_list)
        .set_global_opts(title_opts=opts.TitleOpts(title="深圳北到某一个区间的平均压力"))
    )
    return c


def page_default_layout():
    page = Page()
    page.add(
        pie_qujian(qujian_list,count_list),
        bar_xianlu(xianlu_list,avg_time_list),
        line_szb(short_stations_list, count_list),
    )
    page.render("htmls/智慧地铁综合图表.html")


def conn():
    conn = clickhouse_driver.connect(
        host='master',
        port=9001,
        database='ads',
        user='default',
        password='123456'
    )
    return conn


if __name__ == "__main__":
    conn = conn()
    cursor = conn.cursor()
    cursor.execute('select * from ads.ads_stations_send_passengers_day_top limit 10')
    res = cursor.fetchall()

    qujian_list = []
    count_list = []

    for i in res:
        qujian_list.append(i[1])
        count_list.append(i[2])

    print(qujian_list)
    print(count_list)
    print("================================================================")
    cursor = conn.cursor()
    cursor.execute('select * from ads.ads_line_single_ride_average_time_day_top limit 10')
    res = cursor.fetchall()

    xianlu_list = []
    avg_time_list = []

    for i in res:
        xianlu_list.append(i[1])
        avg_time_list.append(i[2])

    print(xianlu_list)
    print(avg_time_list)
    print("================================================================")
    cursor = conn.cursor()
    cursor.execute("select * from ads_stations_send_passengers_day_top where short_stations like '深圳北-%' order by count desc")
    res = cursor.fetchall()

    short_stations_list = []
    count_list = []

    for i in res:
        short_stations_list.append(i[1])
        count_list.append(i[2])

    print(short_stations_list)
    print(count_list)


    page_default_layout()